﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows;
using System.Windows.Controls;

namespace wpfTool
{
    class PipelineViewManager
    {
        int pipelineViewIndex = 0;

        wpfTool.Window1 MainWindow = null;

        PipelineView selectedPV = null;

        //Array contextMenuSrc;
        //Array contextMenuDst;
        //Array contextMenuShader;

        SortedDictionary<string, PipelineView> pipelineViewDict;

        internal PipelineView CurrentPipeline
        {
            get { return selectedPV; }
        }

        public PipelineViewManager(wpfTool.Window1 wnd)
        {
            MainWindow = wnd;

            pipelineViewDict = new SortedDictionary<string,PipelineView>();

            MainWindow.theTab.SelectionChanged += new SelectionChangedEventHandler(theTab_SelectionChanged);
            this.CreateShaderEvent += new Window1.CreateShaderEventHandler(MainWindow.shaderMgr.OnCreateShaderEventHandler);

            addPipelineView();

            addShader("Basic Src");
            //addShader("Basic Dst");
            //addShader("InverseShader");
            //addShader("TempDstShader");
            //addShader("HistogramViewer");
            //addShader("HistogramViewer");
            //addShader("HistogramViewer");
            //addShader("HSBViewer");
            //addShader("ChannelSplitter");
        }

        void theTab_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            TabItem ti = (e.Source as TabControl).SelectedItem as TabItem;
            if (pipelineViewDict.ContainsKey(ti.Header as string))
            {
                selectedPV = pipelineViewDict[ti.Header as string];
            }
        }

        public void addPipelineView()
        {
            string pipelineName = "Pipeline" + pipelineViewIndex.ToString();
            Canvas canvas = new Canvas();
            canvas.Background = System.Windows.Media.Brushes.DarkGray;
            pipelineViewDict[pipelineName] = new PipelineView(canvas, MainWindow);
            MainWindow.shaderMgr.addPipelineView(pipelineViewDict[pipelineName]);

            System.Windows.Controls.TabItem ti = new System.Windows.Controls.TabItem();
            ti.Content = canvas;
            ti.Header = pipelineName;
            MainWindow.theTab.Items.Add(ti);

            selectedPV = pipelineViewDict[pipelineName];
            selectedPV.AddShaderConnectionEvent += new Window1.PipelineStructureChangeHandler(MainWindow.shaderMgr.OnAddShaderConnectionEventHandler);

            ++pipelineViewIndex;
        }

        public void addShader(string shaderName)
        {
            ShaderBase shader = null;
            CreateShaderEvent(selectedPV, shaderName,out shader);

            ShaderWidget sw = new ShaderWidget(shader, selectedPV);

            // register event
            sw.ShaderWidgetGetFocus += new Window1.ShaderPropertyChangeHandler(MainWindow.propertyGridMgr.OnShaderSelected);
            selectedPV.ShaderWidgetLookUpTable[shader] = sw;

            Canvas.SetTop(sw, 0);
            Canvas.SetLeft(sw, 0);
            selectedPV.canvas.Children.Add(sw);
        }

        public void addShader(string shaderName, int x, int y)
        {
            ShaderBase shader = null;
            CreateShaderEvent(selectedPV, shaderName, out shader);

            ShaderWidget sw = new ShaderWidget(shader, selectedPV);

            // register event
            sw.ShaderWidgetGetFocus += new Window1.ShaderPropertyChangeHandler(MainWindow.propertyGridMgr.OnShaderSelected);
            selectedPV.ShaderWidgetLookUpTable[shader] = sw;

            Canvas.SetTop(sw, y);
            Canvas.SetLeft(sw, x);
            selectedPV.canvas.Children.Add(sw);
        }

        public event Window1.CreateShaderEventHandler CreateShaderEvent;
    }
}
